package com.htudata.zabbix.template.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.Collection;

import com.htudata.core.util.operation.ArrayOperationUtils;
import com.htudata.core.util.operation.CollectionsOperationUtils;
import com.htudata.core.util.operation.StringOperationUtils;
import com.htudata.zabbix.api.service.TemplateZabbixApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TemplateService {

  @Autowired
  private TemplateZabbixApiService templateZabbixApiService;
  
  public static boolean isValidTemplateId(String id) {
    if (StringOperationUtils.isPositiveInt(id))
      return true; 
    return false;
  }
  
  public static boolean containsTemplate(JSONArray jsonArray, String templateName) {
    if (!StringOperationUtils.isValidStr2(templateName))
      return true; 
    if (!CollectionsOperationUtils.isValidCollection((Collection)jsonArray))
      return false; 
    for (Object obj : jsonArray) {
      JSONObject jsonObject = (JSONObject)obj;
      String name = jsonObject.getString("name");
      if (name.contains(templateName))
        return true; 
    } 
    return false;
  }
  
  public JSONArray getTemplates(String[] groupIdArr, String[] hostIdArr, int limit)  throws Exception{
    JSONObject params = new JSONObject();
    if (limit > 0)
      params.put("limit", Integer.valueOf(limit)); 
    params.put("output", new String[] { "templateid", "name" });
    if (ArrayOperationUtils.isValidArr((Object[])groupIdArr))
      params.put("groupids", groupIdArr); 
    if (ArrayOperationUtils.isValidArr((Object[])hostIdArr))
      params.put("hostids", hostIdArr); 
    params.put("selectHosts", new String[] { "hostid" });
    params.put("selectParentTemplates", new String[] { "templateid", "name" });
    params.put("selectItems", "count");
    params.put("selectTriggers", "count");
    params.put("selectGraphs", "count");
    String json = this.templateZabbixApiService.getTemplates(params);
    JSONObject root = JSON.parseObject(json);
    if (root == null)
      return null; 
    JSONArray result = root.getJSONArray("result");
    return result;
  }
  
  public JSONArray getTemplates2(String[] groupIdArr, String[] hostIdArr, int limit) throws Exception {
    JSONObject params = new JSONObject();
    if (limit > 0)
      params.put("limit", Integer.valueOf(limit)); 
    params.put("output", new String[] { "templateid", "name" });
    if (ArrayOperationUtils.isValidArr((Object[])groupIdArr))
      params.put("groupids", groupIdArr); 
    if (ArrayOperationUtils.isValidArr((Object[])hostIdArr))
      params.put("hostids", hostIdArr); 
    String json = this.templateZabbixApiService.getTemplates(params);
    JSONObject root = JSON.parseObject(json);
    if (root == null)
      return null; 
    JSONArray result = root.getJSONArray("result");
    return result;
  }
  
  public JSONArray getTemplateInfo(Long[] templateIdArr, int limit) throws Exception {
    JSONObject params = new JSONObject();
    if (limit > 0)
      params.put("limit", Integer.valueOf(limit)); 
    params.put("output", new String[] { "templateid", "host", "name", "description" });
    if (ArrayOperationUtils.isValidArr((Object[])templateIdArr))
      params.put("templateids", templateIdArr); 
    params.put("selectItems", "count");
    params.put("selectTriggers", "count");
    params.put("selectGraphs", "count");
    String json = this.templateZabbixApiService.getTemplates(params);
    JSONObject root = JSON.parseObject(json);
    if (root == null)
      return null; 
    JSONArray result = root.getJSONArray("result");
    return result;
  }
}
